package com.xsoft;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;

import com.google.gson.Gson;



public class MyMongodb {
	private static final String _uuid = "_uuid";
	
	/**
	 * 创建数据库
	 * 
	 * @param path
	 * @throws Exception
	 */
	public void createDataBase(String path) throws Exception{
		FileOutputStream xmlFile=new FileOutputStream(path);
		
		Document doc=new Document();
		Element root =new Element("database");
		
		Element tb1=new Element("table");
		tb1.setAttribute("name", "test");
		Element tb2=new Element("table");
		tb2.setAttribute("name", "system.indexs");
		Element tb3=new Element("table");
		tb3.setAttribute("name", "system.users");
		
		root.addContent(tb1);
		root.addContent(tb2);
		root.addContent(tb3);
		
		doc.setRootElement(root);
		
		XMLOutputter out=new XMLOutputter();
		out.output(doc, xmlFile);
		
		System.out.println("-------create---database---success-------");
	}
	
	/**
	 * 表插入数据
	 * @param path
	 * @param tbName
	 * @param po
	 * @return
	 * @throws Exception
	 */
	public boolean addData(String path, String tbName, Object po) throws Exception{
		Gson gson=new Gson();
		boolean flag= false;
		
		FileInputStream xmlFile=new FileInputStream(path);
		SAXBuilder saxBuilder=new SAXBuilder();
		Document doc=saxBuilder.build(xmlFile);
		Element root=doc.getRootElement();
		
		List<Element> tables=root.getChildren();
		for (Element table : tables) {
			if(table.getAttributeValue("name").equals(tbName)){//匹配数据表
				Element data=new Element("data");
				
				Map map=new HashMap<String, String>();
				map.put(_uuid, UUID.randomUUID().toString());//模拟数据库生成ID
				
				String json=gson.toJson(po);
				Map mp2=gson.fromJson(json, Map.class);
				map.putAll(mp2); //添加javabean的数据
				
				data.addContent(gson.toJson(map));
				table.addContent(data);
				
				break;
			}
		}
		XMLOutputter out=new XMLOutputter();
		out.output(doc, new FileOutputStream(path));
		flag=true;
		System.out.println("-------insert---data---success-------");
		return flag;
	}
	
	/**
	 * 表修改数据
	 * @param path
	 * @param tableName
	 * @param po
	 * @return
	 * @throws Exception
	 */
	public boolean updateData(String path, String tableName, Object po) throws Exception{
		Gson gson=new Gson();
		boolean flag = false;
		
		FileInputStream xmlFile=new FileInputStream(path);
		SAXBuilder saxBuilder=new SAXBuilder();
		Document doc=saxBuilder.build(xmlFile);
		Element root=doc.getRootElement();
		
		List<Element> tables=root.getChildren();
		
		for (Element table : tables) {
			if(table.getAttributeValue("name").equals(tableName)){ //匹配数据表
				List<Element> datas=table.getChildren();
				for (Element data : datas) {
					Map map1=gson.fromJson(data.getText(), Map.class);
					Map map2=gson.fromJson(gson.toJson(po), Map.class);
					if(map1.get(_uuid).equals(map2.get(_uuid))){//匹配uuid
						map1.putAll(map2);//更新
						Element data_new=new Element("data");
						data_new.setText(gson.toJson(map1));
						table.removeContent(data);
						table.addContent(data_new);
						break;
					}
				}
			}
		}
		
		XMLOutputter out=new XMLOutputter();
		out.output(doc, new FileOutputStream(path));
		flag=true;
		System.out.println("-------update---data---success-------");
		return flag;
	}
	
	/**
	 * 表删除数据
	 * @param path
	 * @param tableName
	 * @param po
	 * @return
	 * @throws Exception
	 */
	public boolean deleteData(String path, String tableName, Object po) throws Exception{
		Gson gson=new Gson();
		boolean flag = false;
		
		FileInputStream xmlFile=new FileInputStream(path);
		SAXBuilder saxBuilder=new SAXBuilder();
		Document doc=saxBuilder.build(xmlFile);
		Element root=doc.getRootElement();
		
		List<Element> tables=root.getChildren();
		
		for (Element table : tables) {
			if(table.getAttributeValue("name").equals(tableName)){ //匹配数据表
				List<Element> datas=table.getChildren();
				for (Element data : datas) {
					Map map1=gson.fromJson(data.getText(), Map.class);
					Map map2=gson.fromJson(gson.toJson(po), Map.class);
					if(map1.get(_uuid).equals(map2.get(_uuid))){//匹配uuid
						table.removeContent(data);
						break;
					}
				}
			}
		}
		
		XMLOutputter out=new XMLOutputter();
		out.output(doc, new FileOutputStream(path));
		flag=true;
		System.out.println("-------delete---data---success-------");
		return flag;
	}
	
	/**
	 * 获取表的全部数据
	 * @param path
	 * @param tableName
	 * @return
	 * @throws Exception
	 */
	public List<Map> loadTableDatas(String path, String tableName) throws Exception{
		List<Map> maps=new ArrayList<Map>();
		Gson gson = new Gson();
		
		FileInputStream xmlFile=new FileInputStream(path);
		SAXBuilder saxBuilder=new SAXBuilder();
		Document doc=saxBuilder.build(xmlFile);
		Element root=doc.getRootElement();
		
		List<Element> tables=root.getChildren();
		for (Element table : tables) {
			if(table.getAttributeValue("name").equals(tableName)){//匹配数据表
				List<Element> datas=table.getChildren();
				for (Element data : datas) {
					String text=data.getText();
					if(text == null){
						break;
					}
					Map map=gson.fromJson(text, Map.class);
					maps.add(map);
				}
			}
		}
		return maps;
	}
}
